<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="流程类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processId">
              <!-- <j-dict-select-tag
                      :disabled="formDisabled"
                      type="list"
                      v-model="model.processId"
                      dictCode="ext_act_process,process_name,id"
                      placeholder="请选择流程类型"
                      @input="selectProcess"
                    /> -->
                    <j-search-select
                        :disabled="formDisabled"
                        v-model="model.processId"
                        dict="ext_act_process,process_name,id"
                        placeholder="请选择流程类型"
                        @change="selectProcess"
                      />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审批节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskId">
              <j-dict-select-tag
                      :disabled="formDisabled || !model.processId"
                      type="list"
                      v-model="model.taskId"
                      :dictCode="processTaskCode"
                      placeholder="请选择审批节点"
                    />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="授权人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="authorization">
              <j-select-user-by-dep :disabled=formDisabled v-model="model.authorization" :multi="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="被授权人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="authorizedUser">
              <j-select-user-by-dep :disabled=formDisabled v-model="model.authorizedUser" :multi="false" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import JSearchSelect from '@/views/scy/components/JSearchSelect.vue'
  import moment from 'moment'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ExtActFlowUserAuthForm',
    components: {
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput,
      JSearchSelect
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        processTaskCode:null,
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          processId: [
            { required: true, message: '请选择流程类型!',whitespace:true},
        ],
        taskId: [
            { required: true, message: '请选择审批节点!',whitespace:true},
        ],
        authorization: [
            { required: true, message: '请选择授权人!',whitespace:true},
        ],
        authorizedUser: [
            { required: true, message: '请选择被授权人!',whitespace:true},
        ],
        },
        url: {
          verifyData:"/api/erp/act/flowUserAuth/extActFlowUserAuth/verifyData",
          add: "/api/erp/act/flowUserAuth/extActFlowUserAuth/add",
          edit: "/api/erp/act/flowUserAuth/extActFlowUserAuth/edit",
          queryById: "/api/erp/act/flowUserAuth/extActFlowUserAuth/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        if(this.model.processId){
          this.processTaskCode="ext_act_process_node,process_node_name,process_node_code,process_id='"+this.model.processId+"' and is_auth='1'";
        }
        this.visible = true;
      },
      selectProcess(data){
        if(data){
          this.processTaskCode="ext_act_process_node,process_node_name,process_node_code,process_id='"+data+"' and is_auth='1'";
          if(this.model.taskId){
            this.$set(this.model,"taskId",null);
          }
        }
      },
      validatorAuth(){
        const that = this;
        return new Promise((resolve,reject)=>{
          getAction(this.url.verifyData,this.model).then((res)=>{
            if(res.success){
              resolve(false);
            }else{
              that.$message.warning(res.message);
              that.$set(this.model,"authorization",null);
              resolve(true);
            }
          }).finally(()=>{
            that.confirmLoading = false;
            that.$emit('unButtonDisable');
          })
        })
      },
      async submitForm(){
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.model.authorization==this.model.authorizedUser){
              that.$message.warning("被授权人不能和授权人一致！");
              that.$set(this.model,"authorizedUser",null);
              that.confirmLoading = false;
              that.$emit('unButtonDisable');
              return;
            }
            this.validatorAuth().then(res=>{
              console.log(res);
              if(!res) {
                httpAction(httpurl,this.model,method).then((res)=>{
                  if(res.success){
                    that.$message.success(res.message);
                    that.$emit('ok');
                  }else{
                    that.$message.warning(res.message);
                    that.$emit('unButtonDisable');
                  }
                }).catch((error)=>{
                  that.$emit('unButtonDisable');
                }).finally(() => {
                  that.confirmLoading = false;
                })
              }
            })
          }else {
           that.$emit('unButtonDisable');
          }

        })
      },
      
    }
  }
</script>
